package com.dhc.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
//加此注解job即为有状态的，即可实现数据的持久化
//@PersistJobDataAfterExecution
public class Job implements org.quartz.Job {
    //声明传递过来的key值,并为其设置set方法,quartz会自动调用set方法为其赋值
    private String jobMsg;
    private String triggerMsg;
    //判断是否为持久化，当不加注解时每次执行此任务都会重新初始化值
    private Integer count;

    public void setJobMsg(String jobMsg) {
        this.jobMsg = jobMsg;
    }

    public void setTriggerMsg(String triggerMsg) {
        this.triggerMsg = triggerMsg;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobKey jobKey = context.getJobDetail().getKey();
        //获取任务信息
        jobKey.getName();//获取任务名
        jobKey.getGroup();//获取组名
        context.getJobDetail().getJobClass().getName();//带路径类名
        context.getJobDetail().getJobClass().getSimpleName();//类名
        System.out.println("执行了");
        //获取任务Map中的值
//        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
//        String jobMsg = jobDataMap.getString("jobMsg");
//        System.out.println(jobMsg);
        //获取触发器Map中的值
//        JobDataMap jobDataMap1 = context.getTrigger().getJobDataMap();
//        String triggerMsg = jobDataMap1.getString("triggerMsg");
//        System.out.println(triggerMsg);
        //获取触发器信息
        TriggerKey triggerKey = context.getTrigger().getKey();
        triggerKey.getName();//获取触发器名称
        triggerKey.getGroup();//获取触发器组名
        //获取其他信息
        context.getFireTime();//获取当前任务执行时间
        context.getNextFireTime();//获取下一个任务执行的时间
        //通过在实现job的类中声明和调用方设置的key值名称相同的参数后,会自动调用set方法,直接获取到对应的value值
        System.out.println(jobMsg);
        System.out.println(triggerMsg);
        ++count;
        System.out.println(count);
        context.getJobDetail().getJobDataMap().put("count",count);
    }

    public static void main(String[] args) throws Exception{
        //Scheduler调度器 工厂中获取调度实例
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        //Job任务实例
        JobDetail jobDetail = JobBuilder.newJob(Job.class)
                .withIdentity("job1", "jobGroup1")//1任务名2组名
                .usingJobData("jobMsg","jobMsg")
                .usingJobData("count",0)
                .build();
//        jobDetail.getKey().getName();//任务名
//        jobDetail.getKey().getGroup();//组名(未设置显示DEFAULT)
//        System.out.println(jobDetail.getJobClass().getName());//com.dhc.quartz.Job
        //Trigger触发器
        Trigger simpleTrigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "triggerGroup1")
                .usingJobData("triggerMsg","SimpleTrigger")
                .startNow()//马上启动触发器
                //每五秒执行一次,只执行3次(结束时间优先于执行次数)
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().repeatSecondlyForever(5).withRepeatCount(2))
                .build();
        //让调度器关联任务和触发器
        scheduler.scheduleJob(jobDetail,simpleTrigger);
        //启动
        scheduler.start();
    }
}
